package com.zsoft.util;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @author zuoshuaiok@gmail.com
 *
 * 2013-4-22
 */
public class ListPropertyAndValueTool {


	public static <T> Map<String, Object> display(final T t, final List<String> fieldNames) {

		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			for (String name : fieldNames) {

				Method m = t.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
				String value = m.invoke(t).toString();
				if(m.getReturnType().toString().equals("class java.util.Date")){
					
					value = sdf.format(sdf.parse(value));
				} 
				map.put(name, value);
			}
			
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	

}
